package com.dushuren.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dushuren.project.common.ErrorCode;
import com.dushuren.project.exception.BusinessException;
import com.dushuren.project.model.dto.SearchWordsRequest;
import com.dushuren.project.model.entity.RecordWordsSearch;
import com.dushuren.project.mapper.RecordWordsSearchMapper;
import com.dushuren.project.model.entity.SuggestWordsSearch;
import com.dushuren.project.model.vo.SuggestWordsSearchVo;
import com.dushuren.project.service.RecordWordsSearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author qieqie
* @description 针对表【record_words_search(搜索记录表)】的数据库操作Service实现
* @createDate 2022-11-02 19:20:10
*/
@Service
public class RecordWordsSearchServiceImpl extends ServiceImpl<RecordWordsSearchMapper, RecordWordsSearch>
    implements RecordWordsSearchService{

  @Resource
  RecordWordsSearchMapper recordWordsSearchMapper;

  /**
   * 查询 Words  id 是否已经存在于 RecordWordsSearch 记录表中 ( 0 数据已存在 1 数据载入成功)
   */
  @Override
  public Long QueryIsRecordWords(Long userId, String information) {
	RecordWordsSearch recordWordsSearch  = new RecordWordsSearch();
	recordWordsSearch.setUserId(userId);
	recordWordsSearch.setSearchInformation(information);;
	QueryWrapper<RecordWordsSearch> queryWrapper = new QueryWrapper<>(recordWordsSearch);
	queryWrapper.select("id"); // 为了介节约数据库查询速度
	Long count = recordWordsSearchMapper.selectCount(queryWrapper);
	if (count == 0) {
	  // 存入 查询表中。
	  this.save(recordWordsSearch);
	  return 1L;
	}
	return 0L;
  }

  @Override
  public Page<SuggestWordsSearchVo> queryListData(SearchWordsRequest searchWordsRequest) {
    String searchInformation = searchWordsRequest.getSearchInformation();
    long current = searchWordsRequest.getCurrent();
    long pageSize = searchWordsRequest.getPageSize();

	QueryWrapper<RecordWordsSearch> queryWrapper = new QueryWrapper<>();
	queryWrapper.select("id", "searchInformation", "userId", "createTime");
	queryWrapper.like(StringUtils.isNotBlank(searchInformation), "searchInformation", searchInformation);
	Page<RecordWordsSearch> page = this.page(new Page<>(current, pageSize), queryWrapper);
	if (page.getRecords().isEmpty()) {
	  return new Page<>();
	}
	List<SuggestWordsSearchVo> collectResult = page.getRecords().stream().map(recordWordsSearch -> {
	  SuggestWordsSearchVo suggestWordsSearchVo = new SuggestWordsSearchVo();
	  suggestWordsSearchVo.setId(recordWordsSearch.getId());
	  suggestWordsSearchVo.setUserId(recordWordsSearch.getUserId());
	  suggestWordsSearchVo.setSearchCategory(2L);
	  suggestWordsSearchVo.setSearchInformation(recordWordsSearch.getSearchInformation());
	  suggestWordsSearchVo.setCreateTime(recordWordsSearch.getCreateTime());
	  return suggestWordsSearchVo;
	}).collect(Collectors.toList());

	Page<SuggestWordsSearchVo> pageSearch = new Page<>(page.getSize(), page.getCurrent(), page.getTotal());
	pageSearch.setRecords(collectResult);

	return pageSearch;
  }

  @Override
  public Long deleteSearchWords(Long searchId) {
	try {
	  this.removeById(searchId);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return 0L;
  }

  @Override
  public Long updateSearchWords(RecordWordsSearch recordWordsSearch) {
	try {
	  this.updateById(recordWordsSearch);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return 0L;
  }

  @Override
  public Long addSearchWords(RecordWordsSearch recordWordsSearch) {
	try {
	  this.save(recordWordsSearch);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return null;
  }
}